package cn.cl.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

// import org.apache.shiro.authz.annotation.RequiresPermissions;
import cn.cl.gulimall.product.entity.ProductAttrValueEntity;
import cn.cl.gulimall.product.service.ProductAttrValueService;
import cn.cl.gulimall.product.vo.AttrGroupRelationVo;
import cn.cl.gulimall.product.vo.AttrRespVo;
import cn.cl.gulimall.product.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import cn.cl.gulimall.product.entity.AttrEntity;
import cn.cl.gulimall.product.service.AttrService;
import cn.cl.common.utils.PageUtils;
import cn.cl.common.utils.R;


/**
 * 商品属性
 *
 * @author 高志红
 * @email 1739745826@qq.com
 * @date 2020-05-07 19:59:24
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {
	@Autowired
	private AttrService attrService;

	/**
	 * 查询
	 */
	@RequestMapping("/{attrType}/list/{catelogId}")
	public R baseAttrList(@RequestParam Map<String, Object> params, @PathVariable("catelogId") Long catelogId, @PathVariable("attrType") String attrType) {
		PageUtils page = attrService.queryBaseAttrPage(params, catelogId, attrType);
		return R.ok().put("page", page);
	}

	/**
	 * 列表
	 */
	@RequestMapping("/list")
	public R list(@RequestParam Map<String, Object> params) {
		PageUtils page = attrService.queryPage(params);

		return R.ok().put("page", page);
	}


	/**
	 * 信息
	 */
	@RequestMapping("/info/{attrId}")
	public R info(@PathVariable("attrId") Long attrId) {
		AttrRespVo respVo = attrService.getAttrInfo(attrId);
		return R.ok().put("attr", respVo);
	}

	/**
	 * 保存
	 */
	@RequestMapping("/save")
	public R save(@RequestBody AttrVo attr) {
		attrService.saveAttr(attr);
		return R.ok();
	}

	/**
	 * 修改
	 */
	@RequestMapping("/update")
	public R update(@RequestBody AttrVo attr) {
		attrService.updateAttr(attr);
		return R.ok();
	}

	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	// @RequiresPermissions("product:attr:delete")
	public R delete(@RequestBody Long[] attrIds) {
		attrService.removeByIds(Arrays.asList(attrIds));

		return R.ok();
	}

	@Autowired
	ProductAttrValueService productAttrValueService;

	/**
	 * SPU规格查询回显
	 */
	@GetMapping("/base/listforspu/{spuId}")
	public R baseAttrlistforspu(@PathVariable("spuId") Long spuId) {
		List<ProductAttrValueEntity> productAttrValueEntitys = productAttrValueService.baseAttrlistforspu(spuId);
		return R.ok().put("data", productAttrValueEntitys);
	}

	/**
	 * 修改
	 */
	@PostMapping("/update/{spuId}")
	public R updateSpuAttr(@PathVariable Long spuId, @RequestBody List<ProductAttrValueEntity> productAttrValueEntities) {
		productAttrValueService.updateSpuAttr(spuId, productAttrValueEntities);
		return R.ok();
	}
}
